package com.niit.galcomment.controller;

import com.netflix.ribbon.proxy.annotation.Http;
import com.niit.entity.Mp;
import com.niit.entity.Sp;
import com.niit.entity.User;
import com.niit.galcomment.service.impl.CommentServiceImpl;
import com.niit.utils.CommonResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.websocket.server.PathParam;
import java.util.List;

@RestController
@RequestMapping("comment")
public class CommentController {
    @Resource
    private CommentServiceImpl commentService;
    @GetMapping("mps")
    public CommonResult<List<Mp>> gemeMp(@PathParam("gid") int gid){
        List<Mp> mps = commentService.findByGid(gid);
        return new CommonResult<List<Mp>>(200,"查询成功", mps);
    }

    @GetMapping("sps")
    public CommonResult<List<Sp>> mpsSp(@PathParam("tid")int tid){
        List<Sp> sps = commentService.findSpByMp(tid);
        return new CommonResult<>(200,"查询成功",sps);
    }

    @GetMapping("deleteMp")
    public CommonResult deleteMp(@PathParam("tid")int tid, HttpSession session){
        Mp mp = commentService.findMp(tid);
        User user = (User) session.getAttribute("user");
        if(!mp.getUid().equals(user.getUid()))
            return new CommonResult(401,"该评论不属于您，无法删除");
        commentService.deleteMp(tid);
        return new CommonResult(200,"删除成功");
    }

    @GetMapping("deleteSp")
    public CommonResult deleteSp(@PathParam("htid")int htid,HttpSession session){
        Sp sp = commentService.findSp(htid);
        User user = (User) session.getAttribute("user");
        System.out.println(user.getUid()+":"+sp.getUid());
        if(!sp.getUid().equals(user.getUid()))
            return new CommonResult(401,"该评论不属于您，无法删除");
        commentService.deleteSp(htid);
        return new CommonResult(200,"删除成功");
    }

    @PostMapping("addMp")
    public CommonResult addMp(@RequestBody Mp mp, HttpSession session){
        User user = (User) session.getAttribute("user");
        mp.setUid(user.getUid());
        commentService.insetMp(mp);
        return new CommonResult(200,"发布成功");
    }

    @PostMapping("addSp")
    public CommonResult addSp(@RequestBody Sp sp,HttpSession session){
        User user = (User) session.getAttribute("user");
        sp.setUid(user.getUid());
        commentService.insetSp(sp);
        return new CommonResult(200,"发布成功");
    }

    @GetMapping("voteMp")
    public CommonResult upvoteMp(@PathParam("tid") int tid){
        commentService.upvoteMp(tid);
        return new CommonResult(200,"点赞成功");
    }

    @GetMapping("voteSp")
    public CommonResult upvoteSp(@PathParam("htid") int htid){
        commentService.upvoteSp(htid);
        return new CommonResult(200,"点赞成功");
    }

}
